﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Base.Security;
using Web.Game.Models;
using Ayatta.Contract.Extension;
using Ayatta.Contract.Component;

namespace Web.Game.Controllers
{
    public class TrialController : GameBaseController
    {
        public TrialController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int param)
        {
            var games = GetGames();
            var identity = User.Identity.Identity();
            var model = new TrialControllerModel.Index();

            var server = games.GetAllServers(true).FirstOrDefault(o => o.Id == param);
            if (server != null && server.JoinTrial)
            {
                model.Server = server;
                model.Trials = GameService.GetGameTrials(param);
                model.Histories = GameService.GetGameTrialHistorys(identity.Id, server.Id);
            }
            model.User = GameService.GetGameUser(identity.Id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Integral(int param)
        {
            var games = GetGames();
            var identity = User.Identity.Identity();
            var result = new SkResult<int>();

            var server = games.GetAllServers(true).FirstOrDefault(o => o.Id == param);
            if (server != null && server.JoinTrial)
            {
                var status = GameService.GetGameTrialIntegral(identity.Id, param);
                if (status == 0)
                {
                    result.Status = true;
                }
                else
                {
                    result.Data = status;
                }
            }
            else
            {
                result.Data = 0;
            }
            return Json(result);
        }
    }
}
